package com.neusoft.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.domain.Employee;
import com.neusoft.domain.ResultCode;
import com.neusoft.domain.ResultPage;
import com.neusoft.exception.BusinessException;
import com.neusoft.mapper.EmployeeMapper;
import com.neusoft.service.EmployeeService;

import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public ResultPage<Employee> findEmployeeByPage(Integer page, Integer pageSize, String account, String nickname, String telephone) {
        // 分页查询 limit 起始索引= (page-1)*pageSize, pageSize

        if(page==null || pageSize == null){
            throw new BusinessException(ResultCode.EMPLOYEE_PAGE,"没有提供当前页或每页显示条数");
        }

        PageHelper.startPage(page,pageSize);

        List<Employee> employeeList =  employeeMapper.selectEmployeeByCondition(account,nickname,telephone);

        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);

        return new ResultPage<Employee>(pageInfo.getList(),pageInfo.getTotal());
    }

    @Override
    public void saveEmployee(Employee employee) {
        if(StringUtils.isEmpty(employee.getAccount())){
            throw new BusinessException(ResultCode.EMPLOYEE_SAVE,"账号是必填字段,请检查是否填写账号");
        }
        // 设置初始密码
        employee.setPassword("123456");
        // 1. 调用一个或者多个dao层方法,实现业务逻辑处理
        employeeMapper.insertEmployee(employee);
    }

    @Override
    public void updateEmployee(Employee employee) {
        if(StringUtils.isEmpty(employee.getAccount())){
            throw new BusinessException(ResultCode.EMPLOYEE_SAVE,"账号是必填字段,请检查是否填写账号");
        }
        // 1. 调用一个或者多个dao层方法,实现业务逻辑处理
        employeeMapper.updateEmployeeById(employee);
    }

    @Override
    public void updateEmployeeStatus(Employee employee) {
        if(employee.getId() == null || employee.getIs_del() == null){
            throw new BusinessException(ResultCode.EMPLOYEE_UPDATE_STATUS,"没有传id或is_del状态字段,修改失败!");
        }
        // 1. 调用一个或者多个dao层方法,实现业务逻辑处理
        employeeMapper.updateEmployeeStatusById(employee);
    }
}
